var app = getApp()
var Utils = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agree: false,
    data_arr: [],
    arr:[],
    inputtext: '',
    upload_picture_list: [],
    key_words: "",
    money:"0",
    typedate: 0,
    time:"",
    dayText:"",
    timeText:"",
    cursor: 0,
    year:'',
    week:'',
    fuwu:[],
    did:"",
    zid:"",
    month_time:'',
    time_leng:"",
    feiyongStare:true,
    tabList: [{
        id: 1,
        text: 'aaa',
        state: false
      },
      {
        id: 2,
        text: 'bbb',
        state: false
      }
    ]
  },
  selzixun(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.dex
    var arr = []
    var state = "authority[" + index + "].state"
    this.setData({
      [state]: !this.data.authority[index].state
    })
    for (var i = 0; i < this.data.authority.length; i++) {
      if (this.data.authority[i].state == true) {
        arr.push(this.data.authority[i])
      }
    }
    
    that.setData({
      arr: arr
    })
  },
  handlePrescriptionSuggestChange() {
    this.setData({
      agree: !this.data.agree
    })

  },
  dayChose:function(e){
    console.log(e)
    this.setData({
      dayText:e.detail.value
    })
  },
  timeChose:function(e){
    console.log(e)
    this.setData({
      timeText:e.detail.value
    })
  },
  handleClickNext(e) {
    var that = this
    var text = that.data.text
    var cursor = that.data.cursor
    var upload_picture_list = that.data.upload_picture_list
    var typedate = that.data.typedate
    var agree = that.data.agree
    var did = that.data.did
    var money = that.data.money
    var arr = that.data.arr
    var money = that.data.money
    var dayText = that.data.dayText
    var timeText = that.data.timeText;
    var time = dayText + ' ' + timeText;
    var hid = that.data.hid;
    var keshi_two = that.data.keshi_two;
    var server = that.data.server;
    var tid = that.data.tid;
    var fuwu = that.data.fuwu;
    var fuwus = "";
    console.log(fuwu);
    for(var i=0;i<fuwu.length;i++)
    {
      if(fuwu[i].state == false)
      {
        fuwu.splice(i,1)
      }else{
        fuwus += fuwu[i].id+','
      }
    }
    fuwus.substring(0,fuwus.length-1)
    var leixing = that.data.leixing;
    var newArry=[]
    for(var i=0;i<arr.length;i++){
      newArry.push(arr[i].text)
    }
    if (arr.length == 0 && did != '') {
      wx.showToast({
        title: '请选择关键字',
        icon: 'none'
      })
      return false;
    }
    if((dayText == '' || timeText == ''))
    {
      wx.showToast({
        title: '请选择预约时间',
        icon: 'none'
      })
      return false;
    }
    if (!agree) {
      var cfstate = 0
    } else {
      var cfstate = 1
    }
    if (cursor == '0') {
      wx.showToast({
        title: '请填写病情描述',
        icon: 'none'
      })
      return false;
    }
    var conet = {
      'text': text,
      'typedate': typedate,
      'upload_picture_list': upload_picture_list,
    }
    if(fuwu.length == '0')
    {
      wx.showToast({
        title: '请选择服务项目',
        icon: 'none'
      })
      return false;
    }
    
    var conets = JSON.stringify(conet)
    var arr = JSON.stringify(newArry)
    var openid = wx.getStorageSync('openid');
    if(openid == '')
    {
      wx.login({
        success(res) {
          console.log(res)
          wx.setStorageSync('code', res.code)
          var code = res.code
          if (res.code) {
            wx.getUserInfo({
              success: function (res) {
                console.log(res)
                var encryptedData = res.encryptedData
                var iv = res.iv
                //3.解密用户信息 获取unionId
                app.util.request({
                  url: "entry/wxapp/authorize.getuid",
                  data: {
                    code: code,
                    encryptedData: encryptedData,
                    iv: iv
                  },
                  success: function (e) {
                    wx.setStorageSync('is_username', false);
                    wx.setStorageSync('sessionKey', e.data.userinfo.session_key)
                    app.globalData.sessionInfo = e.data.userinfo; //用户sessionkey & openid
                    // that.isLogin();
                    wx.setStorageSync('openid', e.data.userinfo.openid)
                    wx.redirectTo({
                      url: '/hyb_yl/userCommunicate/pages/orders/orders?did='+did+"&server="+server+"&money="+money+"&hid="+hid+"&keshi_two="+keshi_two+"&tid="+tid+"&leixing="+leixing + '&conets=' + conets + '&cfstate=' + cfstate + '&arr=' + arr + '&time='+time+'&fuwus='+fuwus+'&zid='+that.data.zid,
                    })
                  }
                })
              },
              fail: function () {
                console.log('获取用户信息失败')
              }
            })
      
          }
        }
      })
    }else{
      wx.redirectTo({
        url: '/hyb_yl/userCommunicate/pages/orders/orders?did='+did+"&server="+server+"&money="+money+"&hid="+hid+"&keshi_two="+keshi_two+"&tid="+tid+"&leixing="+leixing + '&conets=' + conets + '&cfstate=' + cfstate + '&arr=' + arr + '&time='+time+'&fuwus='+fuwus+'&zid='+that.data.zid,
      })
    }
    
  },
  /**   
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var that = this;
    var did = options.did;
    var server = options.server;
    var money = options.money;
    var hid = options.hid;
    var keshi_two = options.keshi_two;
    var tid = options.tid;
    var leixing = options.leixing;
    var name = options.name;
    var zid = options.zid;
    that.setData({
      did:did,
      server:server,
      hid:hid,
      keshi_two:keshi_two,
      tid:tid,
      name:name,
      leixing:leixing,
      zid:zid
    })
    wx.setNavigationBarTitle({
      title: name,
    })
    this.channel = "";
    this.uid = Utils.getUid();
    this.lock = false;
    that.getDocinfo();
    that.getFuwu();
    let userInfo = wx.getStorageSync("userInfo");
    
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo,
        u_name: userInfo.u_name,
        u_thumb: userInfo.u_thumb,
      });
    }
    var that = this
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
  },
  // 获取服务类型
  getFuwu:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.server",
      data:{
        keyword:that.data.server
      },
      success:function(res){
        that.setData({
          fuwu:res.data
        })
      }
    })
  },
  getDocinfo:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/green.dz_info",
      data:{
        id:that.data.did,
      },
      success:function(res){
        var authority = res.data.authority
        var dataArr = []
        for (var i = 0; i < authority.length; i++) {
          dataArr.push({
            text: authority[i],
            state: 0
          })
        }
        that.setData({
          info:res.data,
          authority: dataArr,
        })
        console.log(that.data.info);
        console.log(that.data.authority);
      }
    })
  },
  addMoney:function(e){
    this.setData({
      fuwuStatr:true
    })
  },
  addNum:function(e){
    var index=e.currentTarget.dataset.index
    console.log(index)
    var fuwu=this.data.fuwu
    fuwu[index].num++
    this.TotalFun()
    this.setData({
      fuwu:fuwu
    })
  },
  removeNum:function(e){
    var index=e.currentTarget.dataset.index
    var fuwu=this.data.fuwu
    console.log(fuwu[index].num)
    if(fuwu[index].num==1){
      fuwu[index].num--
        return fuwu[index].state=false,this.TotalFun(),this.setData({
          fuwu:fuwu
        })
    }
    fuwu[index].num--
    this.TotalFun()
    this.setData({
      fuwu:fuwu
    })
  },

  checkChose:function(e){
    var index=e.currentTarget.dataset.index
    console.log(index)
    var fuwu=this.data.fuwu
    fuwu[index].state=!fuwu[index].state
    if(fuwu[index].state){
      fuwu[index].num=1
    }else{
      fuwu[index].num=0
    }
    this.TotalFun()
    this.setData({
      fuwu:fuwu
    })
  },

  // 求总价
  TotalFun:function(){
      var fuwu=this.data.fuwu,s=0;
      for (var i=fuwu.length-1; i>=0; i--) {
        s += fuwu[i].num*fuwu[i].money;
        console.log(fuwu[i].num)
        console.log(fuwu[i].money)
      }
      console.log(s)
      this.setData({
        money:s
      })
  },
  close:function(e){
    var fuwu=this.data.fuwu,arr=[];
    for(var i=0;i<fuwu.length;i++){
        if(fuwu[i].state){
          arr.push(fuwu[i].title)
        }
    }
    var nameText=arr.join(',')
    this.setData({
      feiyongStare:true,
      nameText:nameText
    })
  },
  showFeiyong:function(){
    this.setData({
      feiyongStare:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.names) {
      that.setData({ //将携带的参数赋值
        names: currPage.data.names
      });
    }
    if (currPage.data.j_id) {
      that.setData({ //将携带的参数赋值
        j_id: currPage.data.j_id
      });
    }
    if (currPage.data.tel) {
      that.setData({ //将携带的参数赋值
        tel: currPage.data.tel
      });
    }
    if (currPage.data.region && currPage.data.state == 1) {
      that.setData({ //将携带的参数赋值
        region: currPage.data.region
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  uploadimg: function () {
    let that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.upload_picture_list;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              console.log(res)
              data_arr.push(res.data)
              that.setData({
                upload_picture_list: data_arr,
                typedate: 1
              })
            }
          })
        }
      }
    })
  },

  deleteimg: function (e) {
    var that = this;
    var i = e.currentTarget.dataset.index;
    var imgArr = that.data.data_arr;
    imgArr.splice(i, 1);
    that.setData({
      upload_picture_list: imgArr,
      typedate: 0
    })
  },
  computing_word(e) {
    var that = this
    var inputtext = e.detail.value
    that.setData({
      inputtext: inputtext
    })
  },
  handleContentInput(e) {
    console.log(e)
    var that = this
    that.setData({
      text: e.detail.value,
      cursor: e.detail.cursor
    })
  }
})